﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace OttoEngineSimulation
{
    public partial class SeitenAnsichtForm : Form
    {

        Thread thread;
        private int WaitMilliSeconds = 20;
        delegate void UpdateUI();

        UpdateUI updateUI;



        public SeitenAnsichtForm()
        {
            InitializeComponent();
            this.updateUI = new UpdateUI(this.Refresh);
            this.trackBar1.Maximum = 100;
            this.trackBar1.Minimum = 0;
            float i = 175;
            float winkel = i * (float)System.Math.PI / 50;
            this.seitenAnsichtV2.Winkel = winkel;
            float nockenWelleWinkel = ((i - 25) * (float)System.Math.PI / 50) / 2;
            this.seitenAnsichtV2.NockenWelleWinkel = nockenWelleWinkel;
            this.trackBar1.Value = this.trackBar1.Maximum - 50;
            float dreieckWinkel1 = ((i - 75) * (float)System.Math.PI / 50) / 2;
            this.seitenAnsichtV2.DreiEckWinkel1 = dreieckWinkel1;
            float dreiecKWinkel2 = ((i + 25) * (float)System.Math.PI / 50) / 2;
            this.seitenAnsichtV2.DreiEckWinkel2 = dreiecKWinkel2;
            this.seitenAnsichtV2.ErzeugeStange();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            //  this.seitenZylinder.Init = false;
            this.thread = new Thread(new ThreadStart(this.BerechneKreisDaten2));
            this.thread.Start();
            this.buttonStart.Enabled = false;
            this.button_Stop.Enabled = true;
        }

        private void BerechneKreisDaten2()
        {
            try
            {
                float i = 175.0f;
                while (true)
                {
                    float winkel = i * (float)System.Math.PI / 50;                   
                    float nockenWelleWinkel = ((i - 25) * (float)System.Math.PI / 50) / 2;
                    this.seitenAnsichtV2.NockenWelleWinkel = nockenWelleWinkel;
                    float dreiEckWinkel1 = ((i - 75) * (float)System.Math.PI / 50) / 2;
                    this.seitenAnsichtV2.Winkel = winkel;
                    this.seitenAnsichtV2.DreiEckWinkel1 = dreiEckWinkel1;
                    float dreiecKWinkel2 = ((i + 25) * (float)System.Math.PI / 50) / 2;
                    this.seitenAnsichtV2.DreiEckWinkel2 = dreiecKWinkel2;
                    if (this.InvokeRequired)
                    {
                        Thread.Sleep(this.WaitMilliSeconds);
                        this.Invoke(this.updateUI, null);
                    }
                    i++;
                }
            }
            catch (ObjectDisposedException ex)
            {
                this.Close();
            }
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            this.WaitMilliSeconds = this.trackBar1.Maximum - this.trackBar1.Value;
        }

        private void button_Stop_Click(object sender, EventArgs e)
        {
            this.thread.Suspend();
            this.buttonStart.Enabled = true;
            this.button_Stop.Enabled = false;
        }
    }
}
